﻿using System.IO;
using UnityEngine;
using UnityEngine.Video;

public class MoviePlayer : MonoBehaviour
{
    public VideoPlayer vPlayer;

    private bool isFadeOut = false;

    private float fadeOutTime;

    private float fadeStartTime;

    public string VideoClipName;

    // Start is called before the first frame update
    void Start ()
    {
        if (vPlayer.clip == null && !string.IsNullOrEmpty(VideoClipName))
        {
            if (Application.isEditor)
            {
                var url = "file:///" + Path.Combine(Application.dataPath, "../Movie/" + VideoClipName);
                vPlayer.url = url.Replace("\\", "/");
            }
            else
            {
                if (Application.platform == RuntimePlatform.Android)
                    vPlayer.url = Path.Combine(Application.streamingAssetsPath, VideoClipName);
                else
                    vPlayer.url = "file:///" + Path.Combine(Application.streamingAssetsPath, VideoClipName);
            }
        }

        vPlayer.loopPointReached += EndReached;
        vPlayer.Play();
    }

    void EndReached(VideoPlayer vPlayer)
    {
        //Debug.Log("End reached!");
    }

    public void AddLuaEndReached(VideoPlayer.EventHandler handle)
    {
        vPlayer.loopPointReached += handle;
    }

    public void AddLuaPrepareCompleted(VideoPlayer.EventHandler handle)
    {
        vPlayer.prepareCompleted += handle;
    }


    public void FadeOut(float fadeTime)
    {
        isFadeOut = true;
        fadeOutTime = fadeTime;
        fadeStartTime = Time.time;
    }

}
